对于单元格的背景色,你可以使用你想装饰的相应的颜色列表,或者你可以使用颜色地图按数据值创建一个颜色列表来装饰。对于后一种方法,我尝试了seaborn的调色板和matplotlib的方法。seaborn的调色板被应用于HS列,因为数据是按升序排列的。在一个新的列中,按原顺序重新排序。然后用重新排序的列表来装饰目标列。
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
fig, ax =plt.subplots(1,1)
ax.axis('tight')
ax.axis('off')
table_data = pd.read_csv("test.csv")
table_data =table_data.astype({"Runs":int, "HS":int})
table_data =table_data.sort_values(by='HS')
df = table_data
table = ax.table(cellText=df.values,
colLabels=df.columns,
loc="center",
cellLoc="left")
table.auto_set_column_width(col=list(range(len(df.columns))))
# sns color palette use(data:ascending)
bcmap = sns.mpl_palette("Blues", len(table_data['HS'].unique()))
for idx,b in enumerate(bcmap):
table[(idx+1, 6)].set_facecolor(b)
# matplotlib colormap
from matplotlib import cm
vals = table_data['S/R']
normal = cm.colors.Normalize(vals.min(), vals.max())
bcmap2 = plt.cm.Blues(normal(vals))
for idx, bb in enumerate(bcmap2):
table[(idx+1, 10)].set_facecolor(bb)
plt.show()
![enter image description here](https://i.stack.imgur.com/ERus5.png)
|